package com.hy.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hy.admin.dao.MemberDao;
import com.hy.admin.dao.MemberIncomeDao;
import com.hy.admin.dao.MessageDao;
import com.hy.admin.dao.QuestionDao;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.entity.MemberIncomeEntity;
import com.hy.admin.entity.MessageEntity;
import com.hy.admin.enums.EnumMemberType;
import com.hy.admin.enums.EnumStatus;
import com.hy.admin.helper.response.MessageResponse;
import com.hy.admin.helper.response.PersonalResponse;
import com.hy.admin.service.MessageService;
import com.hy.core.response.ServicesResult;
import com.hy.thread.AddMessageThread;
import com.hy.thread.IOSPushThread;
import com.hy.thread.ThreadPoolService;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by yangqq on 18/4/4.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public ServicesResult<List<MessageResponse>> list(Long memberId, Integer pageNum, Integer pageSize){
        ServicesResult<List<MessageResponse>> servicesResult = new ServicesResult<>();
        //校验请求参数
        if (memberId == null || memberId < 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        //设置分页条件，Parameters:pageNum 页码pageSize 每页显示数量count 是否进行count查询
        if(pageNum == null || pageNum <= 0){
            //初始页码为1
            pageNum = 1;
        }
        if(pageSize == null || pageSize <= 0){
            //初始数量为20
            pageSize = 20;
        }
        PageHelper.startPage(pageNum, pageSize);
        //查询用户消息
        List<MessageResponse> messageList = messageDao.selectByMemberId(memberId);
        //修改消息的查看状态
        messageDao.updateCheckState(memberId);
        servicesResult.setResult(messageList);
        return servicesResult;
    }

    /**
     * 给当前用户之外的人发送消息
     * @param memberId
     */
    @Override
    public Boolean addMessageForCourse(Long memberId) {
        //添加用户消息
        List<Long> idList = memberDao.selectIdList(memberId);
        MemberEntity member = memberDao.selectById(memberId);
        String content = member.getNickName() + "老师上传了新课程，赶快去看看吧！";
        if(idList == null){
            return false;
        }
        List<MessageEntity> messageList = new ArrayList<>();
        MessageEntity message;
        for (Long id : idList){
            message = new MessageEntity();
            message.setMemberId(id);
            message.setContent(content);
            message.setMsgType(1);
            message.setCheckState(0);
            message.setStatus(EnumStatus.VALID.getId());

            Calendar cal = Calendar.getInstance();

            message.setCreateTime(cal.getTime());
            message.setUpdateTime(cal.getTime());

            messageList.add(message);
        }
        int count = messageDao.batchInsert(messageList);
        if(count > 0){
            //启动ios推送线程
            List<String> tokenList = memberDao.selectDeviceToken(memberId);
            List<String> deviceIdList = memberDao.selectAliDeviceId(memberId);
            if(!tokenList.isEmpty() || !deviceIdList.isEmpty()){
                ThreadPoolService.addThreadToCommonPool(new IOSPushThread(content, "新课程通知",tokenList,deviceIdList));
            }
        }else{
            //创建线程后续重新插入数据
            ThreadPoolService.addThreadToCommonPool(new AddMessageThread(memberId));
        }
        return count > 0;
    }

    public Boolean addMessageForOne(Long memberId, String token, String aliDeviceId, String content, String title) {
        //添加用户消息
        if(memberId == null && memberId <= 0){
            return false;
        }
        MessageEntity message = new MessageEntity();
        message.setMemberId(memberId);
        message.setContent(content);
        message.setMsgType(1);
        message.setCheckState(0);
        message.setStatus(EnumStatus.VALID.getId());

        Calendar cal = Calendar.getInstance();

        message.setCreateTime(cal.getTime());
        message.setUpdateTime(cal.getTime());

        int count = messageDao.insertSelective(message);
        if(count > 0){
            if(StringUtils.isNotBlank(token)) {
                //启动ios推送线程
                List<String> tokenList = new ArrayList<>();
                tokenList.add(token);
                ThreadPoolService.addThreadToCommonPool(new IOSPushThread(content, tokenList));
            }
            if(StringUtils.isNotBlank(aliDeviceId)){
                //启动安卓推送线程
                List<String> deviceIdList = new ArrayList<>();
                deviceIdList.add(aliDeviceId);
                ThreadPoolService.addThreadToCommonPool(new IOSPushThread(content, title, deviceIdList));
            }
        }
        return count > 0;
    }

}
